package com.wman.lib.utils.callback;

import android.app.Activity;
import android.app.ProgressDialog;
import android.view.Window;

import com.wman.sheep.okgo.request.BaseRequest;

import androidx.annotation.Nullable;


/**
 * Created by Administrator on 2017/2/14.
 */

public abstract class DialogCallback<T> extends JsonCallback<T> {

    private ProgressDialog dialog;
    private Activity activity;

    private void initDialog(Activity activity) {
        dialog = new ProgressDialog(activity);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setMessage("请求网络中...");
    }

    public void setDialogContent(String content){
        if(content != null){
            dialog.setMessage(content);
        }
    }

    public DialogCallback(Activity activity, T clazz) {
        super(clazz);
        this.activity = activity;
        initDialog(activity);
    }

    @Override
    public void onBefore(BaseRequest request) {
        super.onBefore(request);
        //网络请求前显示对话框
        if (dialog != null && !dialog.isShowing()) {
            if(activity.hasWindowFocus()) {
                dialog.show();
            }
        }
    }

    @Override
    public void onAfter(@Nullable T t, @Nullable Exception e) {
        super.onAfter(t, e);
        //网络请求结束后关闭对话框
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }
}
